home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / TIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-23  |  3.7 KB  |  178 lines

  1. /* "Dumb terminal" session command for serial lines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  *
  4.  *      Feb '91 Bill Simpson
  5.  *              rlsd control and improved dialer
  6.  */
  7. /* mods by PA0GRI */
  8. #include "global.h"
  9. #include "commands.h"
  10. #include "mbuf.h"
  11. #include "proc.h"
  12. #include "iface.h"
  13. #ifdef UNIX
  14. #include "unixasy.h"
  15. #else
  16. #include "n8250.h"
  17. #endif
  18. #include "asy.h"
  19. #include "tty.h"
  20. #include "devparam.h"
  21.  
  22.  
  23. #if !defined(_lint)
  24. static char rcsid[] OPTIONAL = "$Id: tip.c,v 1.15 1996/12/23 20:37:36 root Exp root $";
  25. #endif
  26.  
  27. static void tip_out (int dev,void *n1,void *n2);
  28. static int tipcmd (int argc, char *argv[], void *);
  29.  
  30.  
  31. /* Execute user tip command */
  32. int
  33. tipcmd(argc,argv,p)
  34. int argc OPTIONAL;
  35. char *argv[];
  36. void *p OPTIONAL;
  37. {
  38. struct session *sp = NULLSESSION;
  39. register struct iface *ifp;
  40. struct asy *ap;
  41. char *ifn;
  42. int (*rawsave) (struct iface *,struct mbuf *);
  43. int c;
  44. int usesession = 0;
  45. struct proc *proc1;
  46.  
  47.     if((ifp = if_lookup(argv[1])) == NULLIF){
  48.         tprintf(Badinterface,argv[1]);
  49.         return 1;
  50.     }
  51.     ap = &Asy[ifp->dev];
  52.     if( ifp->dev >= ASY_MAX || ap->iface != ifp ){
  53.         tprintf("Interface %s not asy port\n",argv[1]);
  54.         return 1;
  55.     }
  56.     if(ifp->raw == bitbucket){
  57.         tprintf("tip or dialer session already active on %s\n",argv[1]);
  58.         return 1;
  59.     }
  60.  
  61.     if (Curproc->input == Command->input) {
  62.             usesession = 1;
  63.         /* Allocate a session descriptor */
  64.         if ((sp = newsession (argv[1], TIP, 0)) == NULLSESSION){
  65.             tputs (TooManySessions);
  66.             return 1;
  67.         }
  68.         /* Put tty into raw mode */
  69.         sp->ttystate.echo = 0;
  70.         sp->ttystate.edit = 0;
  71.         (void) sockmode(sp->output,SOCK_BINARY);
  72.     }
  73.  
  74.     /* Save output handler and temporarily redirect output to null */
  75.     rawsave = ifp->raw;
  76.     ifp->raw = bitbucket;
  77.  
  78.     /* Suspend the packet input driver. Note that the transmit driver
  79.      * is left running since we use it to send buffers to the line.
  80.      */
  81.     suspend(ifp->rxproc);
  82. #ifdef POLLEDKISS
  83.     suspend(ap->poller);
  84. #endif
  85.  
  86.     /* Now fork into two paths, one rx, one tx */
  87.     ifn = if_name( ifp, " tip out" );
  88.     proc1 = newproc(ifn,256,tip_out,ifp->dev,NULL,NULL,0);
  89.     if (sp != NULLSESSION)
  90.         sp->proc1 = proc1;
  91.     free( ifn );
  92.  
  93.     ifn = if_name( ifp, " tip in" );
  94.     if (usesession)
  95.         chname( Curproc, ifn );
  96.     free( ifn );
  97.  
  98.     /* bring the line up (just in case) */
  99.     if ( ifp->ioctl != NULL )
  100.         (void) (*ifp->ioctl)( ifp, PARAM_UP, TRUE, 0L );
  101.  
  102.     while((c = get_asy(ifp->dev)) != -1)
  103. #if 1
  104.         tputc (uchar(c));
  105. #else
  106.         tputc(c & 0x7f);
  107. #endif
  108.     tflush();
  109.  
  110.     killproc(proc1);
  111.     ifp->raw = rawsave;
  112.     resume(ifp->rxproc);
  113. #ifdef POLLEDKISS
  114.     resume(ap->poller);
  115. #endif
  116.     if (usesession && sp) {
  117.         sp->proc1 = NULLPROC;
  118.         (void) keywait(NULLCHAR,1);
  119.         freesession(sp);
  120.     }
  121.     return 0;
  122. }
  123.  
  124.  
  125. /* Execute user telnet command */
  126. int
  127. dotip(argc,argv,p)
  128. int argc OPTIONAL;
  129. char *argv[];
  130. void *p OPTIONAL;
  131. {
  132. char **pargv;
  133. int i;
  134.  
  135.     if (Curproc->input == Command->input) {
  136.         /* Make private copy of argv and args,
  137.          * spawn off subprocess and return.
  138.          */
  139.         pargv = (char **)callocw((size_t)argc + 1, sizeof(char *));
  140.         for (i = 0; i < argc; i++)
  141.             pargv[i] = strdup(argv[i]);
  142.         pargv[i] = NULL;
  143.         (void) newproc("tip",256,(void (*)(int,void *,void *))tipcmd,argc,(void *)pargv,p,1);
  144.     } else
  145.         (void) tipcmd(argc,argv,p);
  146.     return 0;
  147. }
  148.  
  149.  
  150. /* Output process, DTE version */
  151. static void
  152. tip_out(dev,n1,n2)
  153. int dev;
  154. void *n1 OPTIONAL,*n2 OPTIONAL;
  155. {
  156.     struct mbuf *bp;
  157.     int c;
  158.  
  159.     while((c = recvchar(Curproc->input)) != EOF){
  160. #ifndef TNOS_68K
  161.         if(c == '\n')
  162. #else
  163.         if(c == '\l')
  164. #endif
  165.             c = '\r';               /* NL => CR */
  166.         bp = pushdown(NULLBUF,1);
  167. #if 1
  168.         bp->data[0] = uchar(c);
  169. #else
  170.         bp->data[0] = (c & 0x7f);
  171. #endif
  172.         (void) asy_send(dev,bp);
  173.         Asy[dev].iface->lastsent = secclock();
  174.     }
  175. }
  176.  
  177.  
  178.